//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for SecurityOptions.
	/// </summary>
	public class SecurityOptions : BaseMembershipPage
	{
		#region Members

		protected YesNoRadioButtonList EnableRolesCookie;
		protected YesNoRadioButtonList EnableAnonymousTracking;
		protected TextBox RolesCookieName;
		protected RequiredFieldValidator Requiredfieldvalidator1;
		protected TextBox RolesCookieExpiration;
		protected RequiredFieldValidator RolesCookieExpirationValidator;
		protected TextBox AnonymousCookieName;
		protected RequiredFieldValidator Requiredfieldvalidator2;
		protected TextBox AnonymousCookieExpiration;
		protected RequiredFieldValidator AnonymousCookieExpirationValidator;
		protected TextBox CookieDomain;
		protected TextBox AnonymousUserOnlineTimeWindow;
		protected RequiredFieldValidator AnonymousUserOnlineTimeWindowValidator;
		protected StatusMessage formStatus;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl Resourcecontrol1;
		protected MPContent DescriptionRegion;
		protected ResourceControl ResourceControl115;
		protected HelpIcon Helpicon2;
		protected ResourceControl ResourceControl116;
		protected HelpIcon Helpicon1;
		protected ResourceControl ResourceControl118;
		protected HelpIcon Helpicon3;
		protected ResourceControl ResourceControl120;
		protected HelpIcon Helpicon4;
		protected ResourceControl ResourceControl122;
		protected HelpIcon Helpicon5;
		protected ResourceControl ResourceControl124;
		protected HelpIcon Helpicon6;
		protected ResourceControl ResourceControl126;
		protected HelpIcon Helpicon7;
		protected ResourceControl ResourceControl128;
		protected HelpIcon Helpicon8;
		protected ResourceControl ResourceControl133;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton btnSave;

		#endregion


		override protected void OnInit(EventArgs e)
		{

			btnSave.Click += new EventHandler(Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
			}

		}

		public override void DataBind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
    
			formStatus.Visible = false;
			EnableRolesCookie.SelectedValue = settings.EnableRoleCookie;
			EnableAnonymousTracking.SelectedValue = settings.EnableAnonymousUserTracking;
			RolesCookieName.Text = settings.RoleCookieName;
			RolesCookieExpiration.Text = settings.RoleCookieExpiration.ToString();

			AnonymousCookieName.Text = settings.AnonymousCookieName;
			AnonymousCookieExpiration.Text = settings.AnonymousCookieExpiration.ToString();
			CookieDomain.Text = settings.CookieDomain.ToString(); 
			AnonymousUserOnlineTimeWindow.Text = settings.AnonymousUserOnlineTimeWindow.ToString();

		}
	
		private void Save_Click(Object sender, EventArgs e) 
		{

			if (!Page.IsValid)
				return;
			
			SaveSettings();

			formStatus.Visible = true;
			formStatus.Success = true;
			formStatus.Text = string.Format(ResourceManager.GetString("CP_Membership_Cache_Warning"), CSContext.Current.SiteSettings.SiteSettingsCacheWindowInMinutes).Replace("'", @"\'");
		}

		private void InitializeComponent()
		{
		
		}


		protected virtual void SaveSettings() 
		{
			SiteSettings settings = CSContext.Current.SiteSettings; 

			settings.EnableRoleCookie = EnableRolesCookie.SelectedValue;
			settings.EnableAnonymousUserTracking = EnableAnonymousTracking.SelectedValue;
			settings.RoleCookieName = RolesCookieName.Text;
			settings.AnonymousCookieName = AnonymousCookieName.Text;
			settings.CookieDomain = CookieDomain.Text; 

			try 
			{ 
				settings.AnonymousCookieExpiration = Int32.Parse( AnonymousCookieExpiration.Text ); 
				if(Int32.Parse( AnonymousCookieExpiration.Text ) < 1)
					AnonymousCookieExpirationValidator.IsValid = false;
			}
			catch { AnonymousCookieExpirationValidator.IsValid = false; }

			try 
			{ 
				settings.RoleCookieExpiration = Int32.Parse( RolesCookieExpiration.Text ); 
				if(Int32.Parse( RolesCookieExpiration.Text ) < 1)
					RolesCookieExpirationValidator.IsValid = false;
			}
			catch { RolesCookieExpirationValidator.IsValid = false; }

			try 
			{ 
				settings.AnonymousUserOnlineTimeWindow = Int32.Parse( AnonymousUserOnlineTimeWindow.Text ); 
				if(Int32.Parse( AnonymousUserOnlineTimeWindow.Text ) < 1)
					AnonymousUserOnlineTimeWindowValidator.IsValid = false;
			}
			catch { AnonymousUserOnlineTimeWindowValidator.IsValid = false; }


			SiteSettingsManager.Save(settings);
			
		}

	
	}
}
